package com.sunfeng.commonlib.polling;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.os.PowerManager;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.TypedValue;


/**
 * Created by iwm on 2017/7/31.
 */

public class Util {
    public static final String APP_FROM = "na-android";//na-android，na-iphone

    public static int getScreenWidth(Context context) {
        if (null != context.getResources() && null != context.getResources().getDisplayMetrics()) {
            return context.getResources().getDisplayMetrics().widthPixels;
        }
        return 0;
    }

    public static int getScreenHeight(Context context) {
        if (null != context.getResources() && null != context.getResources().getDisplayMetrics()) {
            return context.getResources().getDisplayMetrics().heightPixels;
        }
        return 0;
    }

    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int sp2px(Context context, float spValue) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, context.getResources().getDisplayMetrics());
    }

    public static String getCuid(Context context) {
        if (context == null) {
            return "";
        }
//        return CommonParam.getCUID(context);
        return "";
    }

    public static String getIMEI(Context context) {
        if (context == null) {
            return "";
        }
        TelephonyManager manager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        if (manager != null && manager.getDeviceId() != null) {
            return manager.getDeviceId();
        }
        return "";
    }

    public static String getOsVersion() {
        return Build.VERSION.SDK_INT + "," + Build.VERSION.RELEASE;
    }

    public static String getVersion(Context context) {
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pi.versionName;
        } catch (Exception e) {
            return "1.0.0";
        }
    }

    public static String getDeviceModel() {
        return Build.MODEL;
    }

    public static String getScreenWithX(Context context) {
        if (null != getDisplayMetrics(context)) {
            final int height = getDisplayMetrics(context).heightPixels;
            final int width = getDisplayMetrics(context).widthPixels;
            return width + "x" + height;
        }
        return "";
    }

    public static DisplayMetrics getDisplayMetrics(Context context) {
        if (null != context.getResources()) {
            return context.getResources().getDisplayMetrics();
        }
        return null;
    }

    public static void runSafe(Runnable runnable) {
        runSafe(runnable, new Runnable() {
            @Override
            public void run() {
            }
        });
    }

    public static void runSafe(Runnable runnable, Runnable catchRunnable) {
        try {
            if (runnable != null) {
                runnable.run();
            }
        } catch (Throwable t) {
            if (t != null) {
                t.printStackTrace();
            }
            if (catchRunnable != null) {
                catchRunnable.run();
            }
        }
    }

    public static boolean isScreenOn(Context context) {
        PowerManager manager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        if (manager == null) {
            return false;
        }
        return manager.isScreenOn();
    }

}
